layui.define(['table', 'form','my_send'], function(exports){
    var dtree = layui.dtree, 
    layer = layui.layer, 
    $ = layui.jquery, 
    http_proxy = layui.http_proxy;
    var nodes = '';
    var DTree5 =  dtree.render({
      elem: "#menubarTree",
      url: http_proxy('/devStatus/getFMcTree'),
      method: "GET",
      initLevel: 1,
      line: false, // 无树线
      ficon: ["2","-1"],  // 设定一级图标样式。1表示三角形图标，-1表示不设置叶子节点图标
      icon: "-1", // 不设定二级图标样式。
      response:{
        message: "message", //返回信息（必填）
        statusCode: 200, //返回码（必填）
        statusName: "code", //返回标识（必填）
        rootName: "rows",
        treeId: "fMcId", //节点ID（必填）
        parentId:'fParent',
        title: "fMcShowName", //节点名称（必填）
        childName:"children",
        level:'fLevel',
        basicData: "basicData.basicDataList", //用户自定义的记录节点数据
      },
      toolbar:true,
      scroll:"#toolbarDiv5",
      toolbarShow:[], // 默认按钮制空
      toolbarExt:[{toolbarId: "testAdd",icon:"dtree-icon-wefill",title:"增加下级分类",handler: function(node,$div){
        // layer.msg(JSON.stringify(node));
        // 你可以在此添加一个layer.open，里面天上你需要添加的表单元素，就跟你写新增页面是一样的
        layer.open({
          type: 2
          ,title: '增加下级分类'
          ,content: 'addForm.html'
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,success: function(layero, index){
            let iframe = window['layui-layer-iframe'+index];
          }
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            //监听提交
            iframeWindow.layui.form.render();
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-submit)', function(data){
              var field = data.field;
              field.fHasChild = field.fHasChild === '是' ? "1":"0";
              field.fStatus = 1;
              var json = {"fMcId":field.fMcId,"fMcName": field.fMcName,"parentId":node.nodeId,"level":node.level};
              field.fParent = json.parentId;
              field.fLevel = json.level;
              $.ajax({
                url: http_proxy('/materialCaregory/saveMc'),
                type:'post',
                data:field,
                success:function(data){
                  if(data.code = "success"){
                    // initTree();
                    DTree5.partialRefreshAdd($div, json); // 如果是json对象，则会追加元素
                    debugger
                    layer.msg(data.message,{icon:6});
                  }else{
                    layer.msg(data.message,{icon:5});
                  }
                }
              })
              layer.close(index);
                return false;
            });
            submit.trigger('click');
          }
        });
      }
    },
    {toolbarId: "testEdit",icon:"dtree-icon-bianji",title:"编辑分类",handler: function(node,$div){
        layer.msg(JSON.stringify(node.nodeId));
        // 你可以在此添加一个layer.open，里面天上你需要添加的表单元素，就跟你写编辑页面是一样的
        debugger
        layer.open({
          type: 2
          ,title: '编辑分类信息'
          ,content: 'editForm.html?fMcId=' + node.nodeId
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,success: function(layero, index){
            let iframe = window['layui-layer-iframe'+index];
            iframe.initNodeData(node);
          }
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
            //监听提交
            iframeWindow.layui.form.render();
            iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
              var field = data.field;
              field.fHasChild = field.fHasChild === '是' ? "1":"0";
              field.fStatus = 1;
              var json = {"fMcId":field.fMcId,"fMcName": field.fMcName,"parentId":node.parentId,"level":node.level};
              field.fParent = json.parentId;
              field.fLevel = json.level;
              $.ajax({
                url: http_proxy('/materialCaregory/saveMc'),
                type:'post',
                data:field,
                success:function(data){
                  if(data.code = "success"){
                    // initTree();
                    DTree5.partialRefreshEdit($div, json); // 如果是json对象，则会追加元素
                    debugger;
                    layer.msg(data.message,{icon:6});
                  }else{
                    layer.msg(data.message,{icon:5});
                  }
                }
              })
              layer.close(index);
                return false;
            });
            submit.trigger('click');
          }
        })
      }
    },
    {toolbarId: "testDel",icon:"dtree-icon-roundclose",title:"删除分类",handler: function(node,$div){
      // layer.msg(JSON.stringify(node));
      $.ajax({
        url:'http://128.64.90.101:10100/materialCaregory/deleteMc',
        type:'post',
        data:{
          fMcId:node.nodeId
        },
        success:function(data){
          if(data.code = 'success'){
            DTree5.partialRefreshDel($div); // 这样即可删除节点
            debugger
            layer.msg(data.message,{icon:6});
          }else{
            layer.msg(data.message, {icon: 5});
          }
        }
      })
    },
    }]
    });
    
    var $ = layui.$, active = {
      addCategories: function(){
        layer.open({
          type: 2
          ,title: '添加专业分类'
          ,content: 'equipment_classifyform.html'
          ,maxmin: true
          ,area: ['550px', '450px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            //点击确认触发 iframe 内容中的按钮提交
            var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
            submit.click();
          },
          end:function(){
            dtree.reload("menubarTree",{
              url: "http://128.64.90.101:10100/materialCaregory/getFMcTree",
              initLevel: "1",
            });
          }
        });
      }
    };
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    exports('account', {})
});